<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2021/1/19
 * Time: 10:04
 */
namespace App\Service\Api;

use App\Http\Model\Mysql\LimitDiscountModel;
use Illuminate\Support\Carbon;

class LimitService extends BaseService
{
    /**
     * 检查商品是否有折扣
     * @param int $good_id
     * @return array
     */
    public function checkLimit(int $good_id)
    {
        $now_day    =   Carbon::now()->format('Ymd');
        $now_time   =   Carbon::now()->format('Hi');
        $result     =   LimitDiscountModel::where([
            'good_id'   =>  $good_id,
            'limit_day' =>  $now_day,
            'is_active' =>  LimitDiscountModel::IS_ACTIVE,
            'is_delete' =>  LimitDiscountModel::NOT_DELETE
        ])->where('start_time', '<=', $now_time)->where('end_time', '>=', $now_time)->select(['id', 'good_id', 'limit_day', 'start_time', 'end_time', 'discount', 'is_active'])->first();
        if (empty($result)) {
            return [false, []];
        } else {
            return [true, $result->toArray()];
        }
    }

    /**
     * 获取限时折扣商品
     * @param int $page
     * @return array
     */
    public function listLimit(int $page = 1)
    {
        $now_day    =   Carbon::now()->format('Ymd');
        $now_time   =   Carbon::now()->format('Hi');
        $result     =   LimitDiscountModel::with([
            'good' =>  function ($query) {
                $query->select(['id', 'name', 'original_price as sell_price', 'sell_price as original_price', 'cover_pic']);
            }
        ])->where([
            'limit_day' =>  $now_day,
            'is_active' =>  LimitDiscountModel::IS_ACTIVE,
            'is_delete' =>  LimitDiscountModel::NOT_DELETE
        ])->offset(($page - 1) * self::PAGE_LIMIT)->limit(self::PAGE_LIMIT)->select(['id', 'good_id', 'limit_day', 'start_time', 'end_time', 'discount', 'is_active'])->get()->each(function ($item) use ($now_time){
            $item->discount     =   sprintf("%.1f", $item->discount / 10);
            if (!empty($item->good)) {
                $item->good->sell_price     =   sprintf("%.2f", $item->good->original_price * $item->discount / 10);
            }
        });
        if ($result->isEmpty()) {
            return [true, []];
        } else {
            $list   =   $result->toArray();
            $result =   [];
            /*foreach ($list as $key => $item) {
                if ($item['start_time'] <= $now_time && $item['end_time'] >= $now_time) {
                    $item['is_collect']     =   1;
                    $item['left_time']      =   Carbon::createFromFormat('YmdHi', $item['limit_day'] . $item['end_time'])->timestamp - Carbon::now()->timestamp;
                } else {
                    $item['is_collect']     =   0;
                    $item['left_time']      =   0;
                }
                $item['start_time']   =   substr($item['start_time'], 0, 2) . ':' . substr($item['start_time'], 2, 2);
                $item['end_time']     =   substr($item['end_time'], 0, 2) . ':' . substr($item['end_time'], 2, 2);
//                $result[$item['limit_day']][]   =   $item;
                $result[]   =   $item;
            }*/
            foreach ($list as $key => $item) {
                if ($item['start_time'] <= $now_time && $item['end_time'] >= $now_time) {
                    $result['limit_goods'][]    =   $item;
                    $item['left_time']      =   Carbon::createFromFormat('YmdHi', $item['limit_day'] . $item['end_time'])->timestamp - Carbon::now()->timestamp;
                }
                $item['start_time']   =   substr($item['start_time'], 0, 2) . ':' . substr($item['start_time'], 2, 2);
                $item['end_time']     =   substr($item['end_time'], 0, 2) . ':' . substr($item['end_time'], 2, 2);
                $result['limit_time'][$item['end_time']]   =     $item['start_time'] <= $now_time && $item['end_time'] >= $now_time;
            }

            /*$result['limit_time']   =   array_unique($result['limit_time']);
            sort($result['limit_time']);*/
            return [true, $result];
        }
    }
}
